package com.ubs.lingua.buildtool.cs.utils.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import com.ubs.lingua.buildtool.cs.CsBuilderException;

/**
 * Utility class which purpose is to facilitate the Apache Ant tasks launching.
 */
public final class AntLauncher {

    private AntLauncher() {
        // should not be instantiated
    }

    /**
     * Launches the Apache Ant task.
     * @param task the Apache Ant task to be launched.
     */
    public static <T extends Task> void launchTask(final T task) {
        try {
            task.getProject().fireBuildStarted();
            task.getProject().init();
            task.execute();
            task.getProject().fireBuildFinished(null);
        }
        catch (BuildException ex) {
            task.getProject().fireBuildFinished(ex);
            // replacing Ant BuildException type in order to avoid having Ant JAR in client's classpath
            throw new CsBuilderException(ex.getMessage(), ex.getStackTrace()); // NOPMD (PreserveStackTrace): stack trace is copied from original exception
        }
    }

}
